/*
 ============================================================================
 Name		: TheWebAppView.cpp
 Author	  : Vitaly Yevtushenko
 Copyright   : Licensed under GPL V2
 Description : Application view implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include <stringloader.h>
#include <gdi.h>

#include "TheWebAppView.h"
#include "TheWeb_0xE8862163.rsg"

#define DEL(obj) if (obj) { delete obj; obj = NULL; }

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CTheWebAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTheWebAppView* CTheWebAppView::NewL(const TRect& aRect)
	{
	CTheWebAppView* self = CTheWebAppView::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CTheWebAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTheWebAppView* CTheWebAppView::NewLC(const TRect& aRect)
	{
	CTheWebAppView* self = new (ELeave) CTheWebAppView;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

// -----------------------------------------------------------------------------
// CTheWebAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CTheWebAppView::ConstructL(const TRect& aRect)
	{
	// Create a window for this application view
	CreateWindowL();

	// Set the windows size
	SetExtentToWholeScreen();

	//Enable dragging events
	EnableDragEvents();

	// construct data
	iWebData = CWebData::NewL();
	
	//load font
	TFontSpec spec(iCoeEnv->NormalFont()->FontSpecInTwips());
	spec.iFontStyle.SetStrokeWeight(EStrokeWeightNormal);
	User::LeaveIfError(iCoeEnv->ScreenDevice()->GetNearestFontInTwips(iLabelFont, spec));
	
	//load strings
	iCaption = StringLoader::LoadL(R_CAPTION, iCoeEnv);
	iExit = StringLoader::LoadL(R_EXIT, iCoeEnv);
	iPlay = StringLoader::LoadL(R_PLAY, iCoeEnv);
	iEdit = StringLoader::LoadL(R_EDIT, iCoeEnv);
	iAbout = StringLoader::LoadL(R_ABOUT, iCoeEnv);
	
	//set the rects for buttons
	SetRects();
	
	// Activate the window, which makes it ready to be drawn
	ActivateL();
	}

// -----------------------------------------------------------------------------
// CTheWebAppView::CTheWebAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CTheWebAppView::CTheWebAppView()
:iState(EMainMenu)
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CTheWebAppView::~CTheWebAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CTheWebAppView::~CTheWebAppView()
	{
	DEL(iWebData);
	DEL(iCaption);
	DEL(iExit);
	DEL(iPlay);
	DEL(iEdit);
	DEL(iAbout);
	if (iCoeEnv && iLabelFont)
		iCoeEnv->ScreenDevice()->ReleaseFont(iLabelFont);
	}

// -----------------------------------------------------------------------------
// CTheWebAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CTheWebAppView::Draw(const TRect& aRect) const
	{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();

	// Gets the control's extent
	gc.SetBrushColor(KRgbBlack);
	TRect drawRect(Rect());

	// Clears the screen
	gc.Clear(drawRect);
	
	switch (iState)
		{
		case EMainMenu:
			DrawMainMenu(gc, aRect);
			break;
			
		case EGame:
			DrawGame(gc, aRect);
			break;
			
		case EEditor:
			DrawEditor(gc, aRect);
			break;
		}
	}

void CTheWebAppView::DrawGame(CWindowGc& gc, const TRect& aRect) const
	{
	
	}

void CTheWebAppView::DrawEditor(CWindowGc& gc, const TRect& aRect) const
	{
	//grid 
	gc.SetPenColor(TRgb(0x666666));
	
	for (TInt i=40; i<360; i+=40)
		gc.DrawLine(TPoint(i, 0), TPoint(i, 640));

	for (TInt i=40; i<640; i+=40)
		gc.DrawLine(TPoint(0, i), TPoint(360, i));

	//draw lines
	for (TInt i = 0; i < iWebData->LinesCount(); ++i)
		{
		TPoint p1, p2;
		TBool crossed;
		if (KErrNone == iWebData->Line(i, p1, p2, crossed))
			{
			if (crossed)
				gc.SetPenColor(KRgbRed);
			else
				gc.SetPenColor(KRgbWhite);
			gc.DrawLine(p1, p2);
			}
		}

	//draw vertexes
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);

	TPoint radius = TPoint(KVertexRadius, KVertexRadius);
	TPoint center;
	TRect ellipse;
	TBool tapped;

	for (TInt i = 0; i < iWebData->PointsCount(); ++i)
		{
		if (KErrNone == iWebData->Point(i, center, tapped))
			{
			if (tapped)
				{
				gc.SetPenColor(KRgbWhite);
				gc.SetBrushColor(KRgbWhite);
				}
			else
				{
				gc.SetPenColor(KRgbYellow);
				gc.SetBrushColor(KRgbYellow);
				}
			ellipse.iTl = center - radius;
			ellipse.iBr = center + radius;

			gc.DrawEllipse(ellipse);
			
#ifdef _DEBUG
			gc.UseFont(iCoeEnv->NormalFont());
			TBuf<3> d;
			d.Format(_L("%d"), i);
			gc.DrawText(d,center);
			gc.DiscardFont();
#endif
			}
		}
	
	if (iWebData->Solved())
		{
		gc.UseFont(iCoeEnv->NormalFont());
		gc.DrawText(_L("Solved"), TPoint(10,100));
		gc.DiscardFont();
		}
	}

void CTheWebAppView::DrawMainMenu(CWindowGc& gc, const TRect& /*aRect*/) const
	{
	CFont* font = iLabelFont; 
	gc.UseFont(font);
	
	//draw title
	TInt height;
	TInt width = font->MeasureText(*iCaption);
	gc.SetPenColor(KRgbWhite);
	gc.DrawText(*iCaption, TPoint((360 - width) / 2, 40));
	
	//draw buttons 
	TSize ellipse(30,20);
	gc.DrawRoundRect(iRtMenuPlay, ellipse);
	gc.DrawRoundRect(iRtMenuEdit, ellipse);
	gc.DrawRoundRect(iRtMenuAbout, ellipse);
	gc.DrawRoundRect(iRtMenuExit, ellipse);
	
	//draw button labels
	height = font->HeightInPixels();

	width = font->MeasureText(*iPlay);
	gc.DrawText(*iPlay, TPoint((360 - width) / 2, iRtMenuPlay.iTl.iY + (iRtMenuPlay.Height() + height) / 2));
	
	width = font->MeasureText(*iEdit);
	gc.DrawText(*iEdit, TPoint((360 - width) / 2, iRtMenuEdit.iTl.iY + (iRtMenuEdit.Height() + height) / 2));
	
	width = font->MeasureText(*iAbout);
	gc.DrawText(*iAbout, TPoint((360 - width) / 2, iRtMenuAbout.iTl.iY + (iRtMenuAbout.Height() + height) / 2));
	
	width = font->MeasureText(*iExit);
	gc.DrawText(*iExit, TPoint((360 - width) / 2, iRtMenuExit.iTl.iY + (iRtMenuExit.Height() + height) / 2));
	
	gc.DiscardFont();
	}

// -----------------------------------------------------------------------------
// CTheWebAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CTheWebAppView::SizeChanged()
	{
	DrawNow();
	}

// -----------------------------------------------------------------------------
// CTheWebAppView::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// Note: although this method is compatible with earlier SDKs, 
// it will not be called in SDKs without Touch support.
// -----------------------------------------------------------------------------
//
void CTheWebAppView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
	{
	switch (iState)
		{
		case EMainMenu:
			HandleMainMenuPointerEventL(aPointerEvent);
			break;
		
		case EGame:
			HandleGamePointerEventL(aPointerEvent);
			break;

		case EEditor:
			HandleEditorPointerEventL(aPointerEvent);
			break;
		}
	}

void CTheWebAppView::HandleMainMenuPointerEventL(const TPointerEvent& aPointerEvent)
	{
	
	}

void CTheWebAppView::HandleGamePointerEventL(const TPointerEvent& aPointerEvent)
	{
	
	}

void CTheWebAppView::HandleEditorPointerEventL(const TPointerEvent& aPointerEvent)
	{
	if (aPointerEvent.iType == TPointerEvent::EButton1Down)
		{
		if (KErrNotFound != iWebData->PointTapped(aPointerEvent.iPosition))
			{
			iDragging = ETrue;
			DrawDeferred();
			}
		}

	else if (aPointerEvent.iType == TPointerEvent::EDrag && iDragging)
		{
#ifdef _DEBUG
		TPoint p(aPointerEvent.iPosition);
		p.iX /= 20;
		p.iX *= 20;
		p.iY /= 20;
		p.iY *= 20;
		iWebData->DragTo(p);
#else
		iWebData->DragTo(aPointerEvent.iPosition);
#endif
		DrawDeferred();
		}

	else if (aPointerEvent.iType == TPointerEvent::EButton1Up && iDragging)
		{		
		iDragging = EFalse;
		iWebData->StopDrag();
		DrawDeferred();
		}
	
	if (aPointerEvent.iType == TPointerEvent::EButton1Up && aPointerEvent.iPosition.iY > 600)
		{
		TFileName fn(_L("c:\\data\\test.twg"));
		iWebData->SaveL(fn);
		}
	}

void CTheWebAppView::SetRects()
	{
	iRtMenuPlay  = TRect(TPoint(60, 152), TSize(240, 80));
	iRtMenuEdit  = TRect(TPoint(60, 280), TSize(240, 80));
	iRtMenuAbout = TRect(TPoint(60, 408), TSize(240, 80));
	iRtMenuExit  = TRect(TPoint(60, 536), TSize(240, 80));
	}

// End of File
